package huoye.shops.huoye.mine.activity;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.codeideology.android.utils.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.BaseActivity;
import huoye.shops.common.model.SMSCoder;
import huoye.shops.huoye.mine.model.AccountList;
import huoye.shops.huoye.mine.model.AddAccountInfo;
import huoye.shops.huoye.mine.model.EditAccountInfo;
import huoye.shops.huoye.mine.model.PowerList;
import huoye.shops.huoye.mine.presenter.EditAccoundPresenter;
import huoye.shops.huoye.mine.view.IEditAccoundView;
import huoye.shops.huoye.mine.widget.PowerSelectRecyclerView;
import huoye.shops.huoye.operation.view.IEditAddShopView;
import huoye.shops.utils.StockUtils;
import huoye.shops.widget.RoundImageView;

import static huoye.shops.base.BaseFragment.TYPE_START_MANAGE;

public class EditAccoundActivity extends BaseActivity implements IEditAccoundView{


    //手机验证布局
    @BindView(R.id.edit_accound_ll_tel)
    LinearLayout llTel;
    @BindView(R.id.edit_accound_et_accout)
    EditText etAccout;

    @BindView(R.id.edit_accound_et_code)
    EditText etCode;

    @BindView(R.id.edit_accound_btn_get_code)
    Button btnGetCode;

    private String mCodeId = "";
    private String mCode = "";
    private String mMobile = "";


    private EditAccoundPresenter mEditAccoundPresenter;
    private List<PowerList> list;

    @OnClick(R.id.edit_accound_btn_get_code)
    void onGetCodeClick() {
        mMobile = etAccout.getText().toString();
        mEditAccoundPresenter.senCode(mMobile);
    }


    @BindView(R.id.edit_accound_et_name)
    EditText etName;

    @BindView(R.id.edit_accound_rv)
    PowerSelectRecyclerView rv;

    @BindView(R.id.edit_accound_iv_switch)
    ImageView ivSwitch;

/*    @BindView(R.id.edit_accound_iv_binding_wx)
    ImageView ivBindingWx;*/

    @BindView(R.id.edit_accound_rv_head)
    RoundImageView rvHead;

    @BindView(R.id.edit_accound_tv_wx_name)
    TextView tvWxName;
    private AccountList accountList;
    private String mSaveUserId = "";


    @BindView(R.id.edit_accound_btn_save)
    Button btnSave;


    @OnClick(R.id.edit_accound_btn_save)
    void onSaveClick(){
        if(isSaveUser){
            if (isClickSwicht){
                accountState = "1";
            }else {
                accountState = "0";
            }
            mSaveUserId  = accountList.getUserId();

            if(StringUtils.isNotEmpty(mCodeId)){
                mCode = etCode.getText().toString();
            }
         mEditAccoundPresenter.saveAccount(mSaveUserId,accountName,powerStr,accountState,accountList.isEidt(),mCodeId,mCode,mMobile);
        }
    }

    private boolean isSaveUser = false;
    private String powerStr = "" ;
    private String accountName = "";
    private String accountState;
    boolean isClickSwicht = false;
    @OnClick(R.id.edit_accound_iv_switch)
    void onSwitchClick(){
        isClickSwicht = !isClickSwicht;
        ivSwitch.setSelected(isClickSwicht);
        isSaveUser = true;
        btnSave.setBackgroundResource(R.mipmap.img_login_btn);
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_edit_accound;
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {
        accountList = getIntentData();
        accountName = accountList.getWxName();
        mEditAccoundPresenter = new EditAccoundPresenter();
        mEditAccoundPresenter.addTaskListener(this);
        mEditAccoundPresenter.onStart();

        if(accountList.isEidt()){
            setTitle("编辑");
            mEditAccoundPresenter.editAccountInfo(accountList.getUserId());
        }else {
            setTitle("新增账户");
            mEditAccoundPresenter.addAccountInfo();
        }

        if(!StringUtils.isNotEmpty(accountList.getMobile())){
            llTel.setVisibility(View.VISIBLE);
        }

        Glide.with(this)
                .load(accountList.getWxHeadImg())
                .dontAnimate()
                .placeholder(R.mipmap.ic_wait_logo)
                .error(R.mipmap.ic_error)
                .into(rvHead);
        tvWxName.setText(accountList.getWxName());
    }

    @Override
    public void codeByoyomi(String text, boolean IsClickable) {
        if (IsClickable) {
            btnGetCode.setText("重新发送" + (text));
        } else {
            btnGetCode.setText(text);
        }
        btnGetCode.setClickable(!IsClickable);
        btnGetCode.setEnabled(IsClickable);
    }

    @Override
    public void getCodeData(SMSCoder smsCoder) {
        mCodeId = smsCoder.getSendId();
        etCode.requestFocus();
        etCode.setSelection(etCode.getText().length());
    }

    @Override
    public void getCodeStr(AddAccountInfo addAccountInfo) {
        list = addAccountInfo.getPowerList();
        for (int i = 0; i < list.size();i++){
            if(list.get(i).getPowerId().equals("1") || list.get(i).getPowerId().equals("3") ){
                list.remove(i);
            }
        }
        rv.setPowerValue(list);
        rv.setOnPowerSelectItemClick(new PowerSelectRecyclerView.OnPowerSelectItemClick() {
            @Override
            public void onClick() {
                //获取id
                isSaveUser = true;
                btnSave.setBackgroundResource(R.mipmap.img_login_btn);
                powerStr = rv.getParts();
            }
        });

    }



    @Override
    public void getEditAccountInfo(EditAccountInfo editAccountInfo) {
        //	商品状态（-1：已删除 0：下架 1：上架）
        if (editAccountInfo.getiState() == 1) {
            isClickSwicht = true;
            ivSwitch.setSelected(isClickSwicht);
        }

        List<PowerList> powerList = editAccountInfo.getPowerList();
        for (int i = 0; i < powerList.size();i++){
            if(powerList.get(i).getPowerEng().equals("account") || powerList.get(i).getPowerId().equals("1") || powerList.get(i).getPowerId().equals("3")){
                powerList.remove(i);
            }
        }
        rv.setPowerValue(powerList);
        rv.setOnPowerSelectItemClick(new PowerSelectRecyclerView.OnPowerSelectItemClick() {
            @Override
            public void onClick() {
                //获取id
                isSaveUser = true;
                btnSave.setBackgroundResource(R.mipmap.img_login_btn);
                powerStr = rv.getParts();

            }
        });
        List<PowerList> userPower = editAccountInfo.getUserPower();
        List<String> prwerId = new ArrayList<>();
        for (int i  = 0; i < userPower.size();i++ ){
            prwerId.add(userPower.get(i).getPowerId());
        }
        powerStr = StockUtils.listToString(prwerId);
        rv.setSplit(",");
        rv.setValue(powerStr);
    }


}
